<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-07-07 15:58:41
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\QaAbnormal;
use App\Models\QaAbnormalMessage;

/**
 * Summary of QaAbnormalRepository
 * @property QaAbnormalMessage $message
 * @author sunkaiyuan
 */
class QaAbnormalRepository extends Repositories
    {

    protected $relation_models = ['status', 'creator', 'updator', 'material', 'pro_pot' => ['verifor']];

    public function boot(QaAbnormal $model, QaAbnormalMessage $message)
        {
        $this->model   = $model;
        $this->message = $message;
        }
    protected function abnormalCreate($dyelot, $material_id, $color_id, $lot_id, $msg, $creator_id = null)
        {
        $countdyelot = substr_count($dyelot, "-");
        if ($countdyelot == 2) {
            //并缸的，去掉后一个
            $dyelot = substr($dyelot, 0, strripos($dyelot, "-") - 1);
            }

        $abnormal = $this->firstOrCreate([
            'material_id' => $material_id,
            'lot_id'      => $lot_id,
            'color_id'    => $color_id,
            'dyelot'      => $dyelot,
        ]);

        $abnormal->new_message = $msg;
        $abnormal->abncount += 1;
        $abnormal->save();
        return $this->message->create(
            [
                'creator_id'  => $creator_id,
                'created_at'  => date("Y-m-d H:i:s"),
                'message'     => $msg,
                'abnormal_id' => $abnormal->id,
            ]
        );
        }
    }